source  ./env.sh
port=9999
param1=$1
shift
pid=
execPid(){
  pid=`ps -ef|grep "$jarFile"|grep -v grep|awk '{print $2}'`
}
execPid

helpFun(){
echo "example: sh start.sh [cmd] --key value --key2 value2 ... "
echo "[cmd] options [start stop restart deploy]"

}

warnIfStartUp(){
  if [ -n "$pid" ] ;then
    echo 警告,已经启动了一个进程${pid},请检查日志信息的端口是否重复
  fi
}

stopFun(){
if [ -n "$pid" ] ;then
  for p in $pid;do
    echo "准备kill $p"
    kill -9 "$p"
  done
else
 echo "进程未启动"
fi
}

while [ $# -ge 2 ] ;do
 case $1 in
	"--port") port=$2;shift 2;;
 esac
done
cmd="nohup java -Xmx50m -jar -Dserver.port=$port $jarFile"

startFun(){
    echo 1 > /proc/sys/vm/drop_caches
    echo "准备执行：$cmd"
    warnIfStartUp
    $cmd > /dev/null 2>&1 &
    execPid
    echo -e 启动成功:pid=${pid} "查看日志:\ntail -f -n 100 logs/app.log"
}


if [[ $param1 = "--help" ]];then
    helpFun
elif [[ $param1 = "start" ]] ;then
    startFun
elif [[ $param1 = "stop" ]];then
    stopFun
elif [[ $param1 = "restart" ]];then
    stopFun
    startFun
elif [[ $param1 = "deploy" ]];then
    git pull
    mvn package
    stopFun
    startFun
else helpFun
fi


